import { systemLogoImg, xtmcselect } from '@/api/api'
import { imgFilter } from '@/utils/businessTools'
const app = {
  state: {
    sidebar: {
      opened: !+window.sessionStorage.getItem('sidebarStatus'),
      withoutAnimation: false
    },
    device: 'desktop',
    breadData: JSON.parse(sessionStorage.getItem('breadData')) || [], // 面包屑用到的route数据，用于保留query 19-2-15添加
    navcp: '', // navBar中输入公共使用的车牌
    logo: '', // navBar中显示的logo
    activeRoute: 'home', // 当前路由名称
    sysName: '', //  系统名称
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        window.sessionStorage.setItem('sidebarStatus', 1)
      } else {
        window.sessionStorage.setItem('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      window.sessionStorage.setItem('sidebarStatus', 1)
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
    UPDATE_BREAD: (state, routeDatas) => {
      const { matched, query: querys } = routeDatas
      // 删除多余子路由
      if (state.breadData.length > matched.length) {
        state.breadData.splice(matched.length, state.breadData.length - matched.length)
      }
      for (const i in matched) {
        const item = state.breadData[i]
        if (item && matched[i].name === item.name) {
          continue
        } else {
          const { name, path, redirect, meta: { title, isfold }} = matched[i]
          const obj = {
            name,
            path,
            redirect,
            meta: {
              title
            }
          }
          if (redirect) obj.redirect = redirect
          if (isfold) obj.meta.isfold = isfold
          if (parseInt(i) === matched.length - 1) {
            if (querys) {
              obj.query = querys
            }
            state.breadData[i] = obj
            if (state.breadData[parseInt(i) + 1]) {
              state.breadData.splice(parseInt(i) + 1, state.breadData.length - parseInt(i) - 1)
            }
            break
          } else {
            state.breadData[i] = obj
            continue
          }
        }
      }
      sessionStorage.setItem('breadData', JSON.stringify(state.breadData))
    },
    SET_NAVCP: (state, cp) => {
      state.navcp = cp
    },
    SET_LOGO: (state, logo) => {
      state.logo = logo
    },
    SET_ACTIVE_ROUTE(state, name) {
      state.activeRoute = name
    },
    SET_SYS_NAME(state, name) {
      state.sysName = name
    },
  },
  getters: {
    navcp: state => state.navcp,
  },
  actions: {
    async getLogo({ commit }) {
      const res = await systemLogoImg()
      if (res.code == 0) {
        let img = imgFilter(res.data.list)
        if (img.length) {
          commit('SET_LOGO', img[0].path)
        }
      }
    },
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    CloseSideBar({ commit }, { withoutAnimation }) {
      commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    ToggleDevice({ commit }, device) {
      commit('TOGGLE_DEVICE', device)
    },
    async getSysName({commit}) {
      const res = await xtmcselect()
      if (res.code == 0) {
        if (res.data.list && res.data.list.length) {
          let sysName = res.data.list[0].xtmc
          commit('SET_SYS_NAME', sysName)
        }
      }
    },
  }
}

export default app
